## -----------------------------------------------
## Copyright (c) EVE 2005-2009
## -----------------------------------------------
## LEGAL NOTICE: This file is the sole property of
## EMULATION AND VERIFICATION ENGINEERING (EVE).
##
## This file is  provided under  a Product License
## Agreement between EVE and a Licensee.
##
## The Licensee shall not make this file available
## in any  form or  disclose or permit  disclosure
## of this file to third parties.
## -----------------------------------------------

help:
	@echo ""
	@echo "##########################"
	@echo "# Available targets      #"
	@echo "##########################"
	@echo "#                        #"
	@echo "# ZeBu compilation:      #"
	@echo "#  -> compil             #"
	@echo "#                        #"
	@echo "#                        #"
	@echo "# Uart testbench:        #"
	@echo "#  -> run_cb             #"
	@echo "#  -> run_nonblocking    #"
	@echo "#  -> run_blocking       #"
	@echo "#                        #"
	@echo "# UartServer testbench:  #"
	@echo "#  -> run_server         #"
	@echo "#                        #"
	@echo "# UartTerm testbench:    #"
	@echo "#  -> run_xterm          #"
	@echo "#  -> run_xtermcb        #"
	@echo "#                        #"
	@echo "#                        #"
	@echo "# Clean                  #"
	@echo "#  -> clean_compil       #"
	@echo "#  -> clean_run          #"
	@echo "#  -> clean              #"
	@echo "##########################"
	@echo ""
	@echo " For run_server target you can set the followingvariable:"
	@echo " TCP_PORT   : TCP port number (default: 10000)"
	@echo " TCP_MODE   : TCP mode (client or server, default: server)"
	@echo " TCP_SERVER : TCP server for client mode (default: localhost)"
	@echo ""

ifndef ZEBU_IP_ROOT
  $(error ZEBU_IP_ROOT variable not set. Please set ZEBU_IP_ROOT environnment variable)
endif


ZEBUWORK ?= zebu.work
ZCUIWORK ?= zcui.work
ZWORK = zcui.work/zebu.work
ARCH=64
DFEATUREFILE = "./designFeatures"
ZEBU_LIB=Zebu
UART_LIB=Uart

TCP_PORT    ?= 10000
TCP_SERVER  ?= localhost
TCP_MODE    ?= server

ZEBU_FILE_CONF=$(basename $(FILE_CONF)).tcl
TBENCH_SUFFIX      ?= $(word 2,$(subst _, ,$(MAKECMDGOALS)))

# TCP server/client testbench
ifeq ("$(TBENCH_SUFFIX)", "server")
TBENCH = testbench_server
endif

# Xterm testbench
ifeq ("$(TBENCH_SUFFIX)", "xterm")
TBENCH = testbench_xterm
endif

# Xterm testbench
ifeq ("$(TBENCH_SUFFIX)", "xtermcb")
TBENCH = testbench_xterm_cb
endif

# blocking testbench
ifeq ("$(TBENCH_SUFFIX)", "blocking")
TBENCH = testbench_blocking
endif

# blocking testbench
ifeq ("$(TBENCH_SUFFIX)", "nonblocking")
TBENCH = testbench_nonblocking
endif

# blocking testbench
ifeq ("$(TBENCH_SUFFIX)", "cb")
TBENCH = testbench_cb
endif

# default testbench
TBENCH ?= testbench_cb

ifdef USE_DRIVER_ITERATOR
 DRIVER_TER_OPT = -DUSE_DRIVER_ITERATOR=YES
endif

############################
## Simulation
############################
sim:
	vlogan \
	-sverilog -full64 \
	-no_error MPD \
	-f test.f_vcs \
	+incdir+/home2/zebu/workspace/mars/4c/mars.4c/cmc_sid/design/mcu/rtl \
	+incdir+/home2/zebu/workspace/mars/4c/mars.4c/cmc_sid/iplibs/ddr3_phy/rtl/controller \
	+define+ZEBU_EVAL \
	./mem/zebu_ram.v ./mem/zebu_2pram.v ./mem/zebu_ddr_mem.v \
	+define+ZEBU_EVAL_CG1 \
	+define+ZEBU_EVAL_PAD \
	+define+ZEBU_EVAL_NMOS \
	+define+ZEBU_EVAL_PMOS \
	+define+ZEBU_EVAL_TR \
	+define+ZEBU_EVAL_DDR \
	+define+ZEBU_EVAL_CMC \
	+define+ZEBU_EVAL_MARS \
	+define+PXP_SINGLE_DIR_PAD  \
	+define+PXP_I_uart_0_rxd_pad \
	+define+PXP_O_uart_0_txd_pad \
	./top.v
	vcs chip_tb_wrapper -l vcs_sim_compile.log

############################
## COMPILATION
############################
compile:
	 ZCUIWORK="${ZCUIWORK}" REMOTECMD="${REMOTECMD}" zCui -u ./4c.utf --nogui --compile -w ${ZCUIWORK}

compile_gui:
	ZCUIWORK="${ZCUIWORK}" REMOTECMD="${REMOTECMD}" zCui -u ./4c.utf -w ${ZCUIWORK}

clean_compil:
	rm -rf work zcuiUC.work


############################
## EMULATION 
############################

############################
CXX       = g++
INCL_PATH = -I$(ZEBU_ROOT)/include -I$(ZEBU_IP_ROOT)/include -I../tb  -I/opt/zebu/ZEBU_IP_ROOT/XTOR/JTAG_T32.2.2/include
LIB_PATH  = -m$(ARCH) -L$(ZEBU_ROOT)/lib -l$(ZEBU_LIB)  -L$(ZEBU_IP_ROOT)/lib -l$(UART_LIB)  -L/opt/zebu/ZEBU_IP_ROOT/XTOR/JTAG_T32.2.2/lib64 -lt32_Jtag_Server_ZeBu
CXXFLAGS  = -m$(ARCH) -O $(INCL_PATH) -Wall -fexceptions                                     \
            -DZWORK=\"../$(ZWORK)\"  -DDFEATURES=\"../$(DFEATUREFILE)\"                      \
            -DTCP_MODE=\"$(TCP_MODE)\" -DTCP_SERVER=\"$(TCP_SERVER)\" -DTCP_PORT=$(TCP_PORT) \
            $(DRIVER_TER_OPT) 

############################
rundir :
	mkdir rundir

rundir/testbench.o : rundir ./testbench_xterm.cc
	cd rundir && $(CXX) $(CXXFLAGS) -c ../testbench_xterm.cc -o ./testbench.o

rundir/testbench : $(ZEBU_IP_ROOT)/lib/lib$(UART_LIB).so rundir/testbench.o
	cd rundir && $(CXX) $(LIB_PATH) -o testbench testbench.o
	rm rundir/testbench.o

rundir_jtag: ./1
	cd rundir_jtag && $(CXX) $(CXXFLAGS) -c ../testbench_xterm.cc -o ./testbench.o
	cd rundir_jtag && $(CXX) $(LIB_PATH) -o testbench testbench.o
	rm rundir_jtag/testbench.o

run: rundir rundir/testbench
	cd rundir && ZEBU_USE_SCHEDULER=OK && LD_LIBRARY_PATH=$(ZEBU_IP_ROOT)/lib:$$LD_LIBRARY_PATH && zRun -testbench "./testbench"

run_r: rundir rundir/testbench
	cd rundir && ZEBU_USE_SCHEDULER=OK && LD_LIBRARY_PATH=$(ZEBU_IP_ROOT)/lib:$$LD_LIBRARY_PATH && zRun -testbench "./testbench -r"

run_jtag: rundir_jtag 
	cd rundir_jtag && ZEBU_USE_SCHEDULER=OK && LD_LIBRARY_PATH=$(ZEBU_IP_ROOT)/lib:$$LD_LIBRARY_PATH && zRun -testbench "./testbench"

run_gdb: rundir rundir/testbench
	cd rundir && ZEBU_USE_SCHEDULER=OK && LD_LIBRARY_PATH=$(ZEBU_IP_ROOT)/lib:$$LD_LIBRARY_PATH && gdb ./testbench

dpi.o: ./print_count.cc
	g++ -fPIC -o dpi.o -c print_count.cc \
	-I$(ZEBU_ROOT)/include -I../zcui.work/zebu.work

dpi.so: dpi.o
	g++ -shared -o dpi.so dpi.o

#ifdef DEBUG
DEBUGDRIVERCLK = -debugDriverClk
#endif

run_gui: rundir rundir/testbench
	cd rundir && ZEBU_USE_SCHEDULER=OK && LD_LIBRARY_PATH=$(ZEBU_IP_ROOT)/lib:$$LD_LIBRARY_PATH && zRun -testbench './testbench'


run_cb run_nonblocking run_blocking run_server run_xterm run_xtermcb: run
run_cb_gdb run_nonblocking_gdb run_blocking_gdb run_server_gdb run_xterm_gdb: run_gdb
run_cb_gui run_nonblocking_gui run_blocking_gui run_server_gui run_xterm_gui: run_gui

clean_run:
	rm -rf rundir

############################
## CLEAN
############################
clean: clean_run

.PHONY: compil run clean clean_run clean_compil run_gui run_gdb
